<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item label="姓名:">
            <el-input
              v-model.trim="queryForm.username"
              placeholder="请输入人员名称"
              clearable
            />
          </el-form-item>
          <el-form-item label="手机号码:">
            <el-input
              v-model.trim="queryForm.phoneNumber"
              placeholder="请输入手机号码"
              clearable
            />
          </el-form-item>

          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
      <vab-query-form-left-panel :span="24">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          新增用户
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="username"
        label="用户名"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="phoneNumber"
        label="手机号码"
      ></el-table-column>

      <el-table-column
        show-overflow-tooltip
        prop="roleName"
        label="角色"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="loginLocked"
        label="登录状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.loginLocked" type="danger">
            <i class="el-icon-lock"></i>
            已锁定
          </el-tag>
          <el-tag v-else>正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="400">
        <template #default="{ row }">
          <el-button
            type="warning"
            icon="el-icon-edit"
            @click="handleResetPwd(row)"
          >
            重置密码
          </el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
          <el-button
            v-if="row.loginLocked"
            type="success"
            icon="el-icon-unlock"
            @click="handleUnlock(row)"
          >
            解除锁定
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <edit ref="edit" @fetch-data="fetchData"></edit>
  </div>
</template>

<script>
import { getList, doDelete, updatePwd, updateLocked } from '@/api/user'
import Edit from './components/UserManagementEdit'

export default {
  name: 'UserManagement',
  components: { Edit },
  data() {
    return {
      list: null,
      roomList: null,
      listLoading: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      selectRows: '',
      elementLoadingText: '正在加载...',
      queryForm: {
        pageNo: 1,
        pageSize: 10,
        username: '',
        phoneNumber: '',
      },
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    setSelectRows(val) {
      this.selectRows = val
    },
    handleEdit(row) {
      if (row.id) {
        this.$refs['edit'].showEdit(row)
      } else {
        this.$refs['edit'].showEdit()
      }
    },
    handleUnlock(row) {
      let _this = this
      this.$baseAlert('确定解锁该用户吗', '解锁用户', async function () {
        await updateLocked(row.id)
        _this.$baseMessage('该用户已解锁', 'success')
        _this.fetchData()
      })
    },
    handleDelete(row) {
      if (row.id) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          await doDelete(row.id)
          this.$baseMessage('用户已被删除', 'success')
          this.fetchData()
        })
      }
    },
    handleResetPwd(row) {
      let _this = this
      this.$prompt('请输入密码', '修改密码', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /.+/,
        inputType: 'password',
        inputErrorMessage: '密码不能为空',
      })
        .then(async ({ value }) => {
          await updatePwd({
            id: row.id,
            password: value,
          })

          this.$message({
            type: 'success',
            message: '密码更新成功',
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入',
          })
        })
    },
    handleSizeChange(val) {
      this.queryForm.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNo = val
      this.fetchData()
    },
    queryData() {
      this.queryForm.pageNo = 1
      this.fetchData()
    },
    async fetchData() {
      this.listLoading = true
      const { records, totalElement } = await getList(this.queryForm)
      this.list = records
      this.total = totalElement
      setTimeout(() => {
        this.listLoading = false
      }, 300)
    },
  },
}
</script>
